package com.canbot.u05.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zmp on 2017/9/20.
 */

public abstract class BasePageAdapter extends PagerAdapter {

        protected BasePageAdapter(List<Integer> list) {
                this.list = list;
        }

        protected BasePageAdapter(int layout,int count) {
                this.list = new ArrayList<>();
                for (int i = 0; i < count; i++) {
                        list.add(layout);
                }
        }

        private List<Integer> list;

        /**
         * page复用
         */
        private HashMap<Integer, View> hashMap = new HashMap<>();

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {

                return arg0 == arg1;
        }

        @Override
        public int getCount() {
                return list.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
                hashMap.put(position, (View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
                View inflate = hashMap.get(position);
                if (inflate == null) {
                        LayoutInflater layoutInflater = LayoutInflater.from(container.getContext());
                        inflate = layoutInflater.inflate(list.get(position), container, false);
                        setPageView(inflate, position, list.get(position), list.get(position));
                }
                container.addView(inflate);
                return inflate;
        }

        public abstract void setPageView(View inflate, int position, int layoutID, Object o);
}
